<script setup lang="ts">
import { ref, watch } from 'vue'
import { useUser } from './hook'

import { useRenderIcon } from '@/components/ReIcon/src/hooks'
import { useTableHeight } from '@/hooks/useTableHeight'
import DialogForm from './components/dialog-form.vue'
import ImportDialogForm from './components/import-dialog-form.vue'
import Search from '@iconify-icons/ep/search'
import Refresh from '@iconify-icons/ep/refresh'
import AddFill from '@iconify-icons/ri/add-circle-line'
import Delete from '@iconify-icons/ri/delete-bin-2-line'
import Update from '@iconify-icons/ri/edit-box-line'
import CircleCheck from '@iconify-icons/ri/checkbox-circle-line'
import Key from '@iconify-icons/ri/key-2-line'
import Download from '@iconify-icons/ri/download-line'
import Upload from '@iconify-icons/ri/upload-line'
defineOptions({
  name: 'User',
})

const formRef = ref()
const {
  form,
  title,
  userId,
  loading,
  columns,
  deptName,
  dataList,
  multiple,
  dateRange,
  pagination,
  deptOptions,
  postOptions,
  roleOptions,
  deptTreeRef,
  queryParams,
  showSelection,
  sys_normal_disable,
  formDialogVisible,
  uploadDialogVisibie,
  onSearch,
  resetForm,
  handleAdd,
  filterNode,
  handleUpdate,
  handleDelete,
  handleExport,
  handleImport,
  handleResetPwd,
  handleAuthRole,
  handleNodeClick,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
} = useUser()
const { tableHeight, tableRef } = useTableHeight()

/** 根据名称筛选部门树 */
watch(
  () => deptName.value,
  (val) => {
    deptTreeRef.value.filter(val)
  }
)
</script>

<template>
  <div class="main h-full">
    <el-row class="h-full" :gutter="20">
      <!--部门数据-->
      <el-col :span="6" :xs="24">
        <div class="head-container">
          <el-input
            v-model="deptName"
            placeholder="请输入部门名称"
            clearable
            prefix-icon="Search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-scrollbar ref="scrollTree" :height="tableHeight">
            <el-tree
              ref="deptTreeRef"
              :data="deptOptions"
              :props="{ label: 'label', children: 'children' }"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              node-key="id"
              highlight-current
              default-expand-all
              @node-click="handleNodeClick"
            />
          </el-scrollbar>
        </div>
      </el-col>
      <el-col :span="18" :xs="24">
        <div class="table-view-layout">
          <el-form
            ref="formRef"
            :inline="true"
            :model="queryParams"
            class="bg-el-bg w-[99/100] pl-8 pt-4"
          >
            <el-form-item label="用户名称" prop="userName">
              <el-input
                v-model="queryParams.userName"
                placeholder="请输入用户名称"
                clearable
                style="width: 240px"
                @keyup.enter="onSearch"
              />
            </el-form-item>
            <el-form-item label="手机号码" prop="phonenumber">
              <el-input
                v-model="queryParams.phonenumber"
                placeholder="请输入手机号码"
                clearable
                style="width: 240px"
                @keyup.enter="onSearch"
              />
            </el-form-item>
            <el-form-item label="状态" prop="status">
              <el-select
                v-model="queryParams.status"
                placeholder="用户状态"
                clearable
                style="width: 240px"
              >
                <el-option
                  v-for="dict in sys_normal_disable"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="创建时间" style="width: 308px">
              <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                :icon="useRenderIcon(Search)"
                :loading="loading"
                @click="onSearch"
              >
                搜索
              </el-button>
              <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)">
                重置
              </el-button>
            </el-form-item>
          </el-form>
          <PureTableBar title="用户管理">
            <template #buttons>
              <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="handleAdd">
                新增
              </el-button>
              <el-button
                type="primary"
                :icon="useRenderIcon(Delete)"
                :disabled="multiple"
                @click="handleDelete"
                >删除</el-button
              >
              <el-button type="primary" :icon="useRenderIcon(Upload)" @click="handleImport">
                导入
              </el-button>
              <el-button type="primary" :icon="useRenderIcon(Download)" @click="handleExport">
                导出
              </el-button>
            </template>
            <template #table>
              <pure-table
                ref="tableRef"
                border
                align-whole="center"
                showOverflowTooltip
                table-layout="auto"
                :loading="loading"
                :max-height="tableHeight"
                :checkList="showSelection ? ['勾选列'] : []"
                :data="dataList"
                :columns="columns"
                :pagination="pagination"
                :header-cell-style="{
                  background: 'var(--el-table-row-hover-bg-color)',
                  color: 'var(--el-text-color-primary)',
                }"
                @selection-change="handleSelectionChange"
                @page-size-change="handleSizeChange"
                @page-current-change="handleCurrentChange"
              >
                <template #operation="{ row }">
                  <el-tooltip v-if="row.userId !== 1" content="修改" placement="top">
                    <el-button
                      class="reset-margin"
                      link
                      type="primary"
                      :icon="useRenderIcon(Update)"
                      @click="handleUpdate(row)"
                    />
                  </el-tooltip>
                  <el-tooltip v-if="row.userId !== 1" content="删除" placement="top">
                    <el-button
                      class="reset-margin"
                      link
                      type="primary"
                      :icon="useRenderIcon(Delete)"
                      @click="handleDelete(row)"
                    />
                  </el-tooltip>
                  <el-tooltip v-if="row.userId !== 1" content="重置密码" placement="top">
                    <el-button
                      class="reset-margin"
                      link
                      type="primary"
                      :icon="useRenderIcon(Key)"
                      @click="handleResetPwd(row)"
                    />
                  </el-tooltip>
                  <el-tooltip v-if="row.userId !== 1" content="分配角色" placement="top">
                    <el-button
                      class="reset-margin"
                      link
                      type="primary"
                      :icon="useRenderIcon(CircleCheck)"
                      @click="handleAuthRole(row.userId)"
                    />
                  </el-tooltip>
                </template>
              </pure-table>
            </template>
          </PureTableBar>
        </div>
      </el-col>
    </el-row>
    <DialogForm
      v-model:visible="formDialogVisible"
      :title="title"
      :data="form"
      :userId="userId"
      :deptOptions="deptOptions"
      :postOptions="postOptions"
      :roleOptions="roleOptions"
      @refresh="onSearch"
    />
    <ImportDialogForm v-model:visible="uploadDialogVisibie" :title="title" @refresh="onSearch" />
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
